package model;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.ini4j.Wini;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author pyin
 */
public class VMMachineFactory {

    private static Wini cfg = null;


    static {
        try {
            cfg = new Wini(new File(ConfigManager.getVMMachineCfgFile()));
        } catch (IOException ex) {
            Logger.getLogger(VMMachineFactory.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static VMMachine getVMMachine(String name) throws InvalidVMHostNameException {
        if (isMachineValid(name)) {
            VMMachine machine = new VMMachine();
            machine.setName(name);
            machine.setOs(cfg.get(name, "OS"));
            machine.setVmFileName(cfg.get(name, "VM_FILE"));
            machine.setVmHost(cfg.get(name, "VM_HOST"));
            machine.setVmSnapShot(cfg.get(name, "VM_SNAPSHOT"));
            machine.setVmHost(cfg.get(name, "VM_HOST"));
            machine.setTsProjectPath(cfg.get(name, "SCRIPT_PATH"));
            machine.setTsProjectArchive(cfg.get(name, "SCRIPT_ARCHIVE"));
            return machine;
        }else{
            throw new InvalidVMHostNameException();
        }

    }

    private static boolean isMachineValid(String name) {
        try {
            FileReader fr = new FileReader(ConfigManager.getVMMachineCfgFile());
            BufferedReader br = new BufferedReader(fr);
            String line;
            Pattern pattern = Pattern.compile("\\[([^\\[\\]]+)\\]");
            while ((line = br.readLine()) != null) {
                Matcher m = pattern.matcher(line);
                if (m.find()) {
                    if (m.group(1).equalsIgnoreCase(name)) {
                        return true;
                    }
                }
            }

        } catch (IOException ex) {
            Logger.getLogger(VMMachineFactory.class.getName()).log(Level.SEVERE, null, ex);
        }
//        System.out.println(result);
        return false;
    }

    public static List getVMMachines() {
        ArrayList result = new ArrayList();
        try {
            FileReader fr = new FileReader(ConfigManager.getVMMachineCfgFile());
            BufferedReader br = new BufferedReader(fr);
            String line;
            Pattern pattern = Pattern.compile("\\[([^\\[\\]]+)\\]");
            while ((line = br.readLine()) != null) {
                Matcher m = pattern.matcher(line);
                if (m.find()) {
                    result.add(getVMMachine(m.group(1)));
                }
            }

        } catch (IOException ex) {
            Logger.getLogger(VMMachineFactory.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvalidVMHostNameException e) {
        }
//        System.out.println(result);
        return result;
    }

    public static void main(String[] args) {
        try {
            VMMachine machine = VMMachineFactory.getVMMachine("win03uepinstall");
            System.out.println(machine.getOs());
            System.out.println(machine.getVmFileName());
            VMMachineFactory.getVMMachines();
        } catch (InvalidVMHostNameException ex) {
            Logger.getLogger(VMMachineFactory.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
